{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "862af0b5",
   "metadata": {},
   "source": [
    "#### NumPy练习题1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "73729d97",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d9b0932e",
   "metadata": {},
   "source": [
    "1. 创建一个长度为10的一维全为0的ndarray对象，然后让第五个元素等于1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f869f21e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = np.zeros(10, dtype=np.int32)\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f9ac9b8f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0, 0, 1, 0, 0, 0, 0, 0])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n[4] = 1\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f844030",
   "metadata": {},
   "source": [
    "2. 创建一个元素为从10到49的ndarray对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a4a1eeab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,\n",
       "       27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,\n",
       "       44, 45, 46, 47, 48, 49])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = np.arange(10, 50)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a814967",
   "metadata": {},
   "source": [
    "3. 将第2题的所有元素位置反转"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6a083732",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33,\n",
       "       32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16,\n",
       "       15, 14, 13, 12, 11, 10])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n[::-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84b72bda",
   "metadata": {},
   "source": [
    "4. 使用np.random.random创建一个10*10的ndarray对象，并打印出最大最小元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "53e2bfbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.80858926, 0.57478412, 0.4309132 , 0.4443954 , 0.97267218,\n",
       "        0.21828123, 0.44802477, 0.91063521, 0.36864744, 0.37144194],\n",
       "       [0.66600911, 0.23189176, 0.12499666, 0.50421062, 0.7770379 ,\n",
       "        0.94551872, 0.80936868, 0.29213386, 0.47019547, 0.68859951],\n",
       "       [0.97763443, 0.87560221, 0.11482377, 0.4462742 , 0.20478856,\n",
       "        0.27154378, 0.66659245, 0.61549895, 0.66464722, 0.56764114],\n",
       "       [0.97155596, 0.83210471, 0.23713051, 0.09521287, 0.04821757,\n",
       "        0.19559308, 0.24375971, 0.46901881, 0.51608813, 0.2918764 ],\n",
       "       [0.0292426 , 0.46661271, 0.55871897, 0.88767966, 0.96904447,\n",
       "        0.04043004, 0.42159444, 0.7360681 , 0.83380147, 0.17677575],\n",
       "       [0.84367229, 0.91291067, 0.60876948, 0.52591317, 0.58331704,\n",
       "        0.92613884, 0.83947151, 0.00752762, 0.80283566, 0.61298178],\n",
       "       [0.3721608 , 0.10522197, 0.73760873, 0.04785154, 0.39512256,\n",
       "        0.51976147, 0.82792179, 0.98578974, 0.59797662, 0.15583616],\n",
       "       [0.30195925, 0.65830608, 0.53544539, 0.78515669, 0.66897889,\n",
       "        0.99698187, 0.69066575, 0.41700979, 0.7362812 , 0.6414072 ],\n",
       "       [0.28654324, 0.58697929, 0.3357566 , 0.19956326, 0.47126447,\n",
       "        0.8317732 , 0.9505175 , 0.80767771, 0.72442301, 0.51122091],\n",
       "       [0.06023063, 0.65980217, 0.01703103, 0.75117906, 0.12509361,\n",
       "        0.87890109, 0.25890768, 0.8658428 , 0.59817722, 0.96425859]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "0.9969818687190157"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "0.007527619624490001"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n = np.random.random(size=(10, 10))\n",
    "display(n, np.max(n), np.min(n))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbabef86",
   "metadata": {},
   "source": [
    "5. 创建一个10*10的ndarray对象，且矩阵边界全为1，里面全为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "c5c55df5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int16)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方式1\n",
    "n = np.zeros((10, 10), dtype=np.int16)\n",
    "n[[0, -1]] = 1\n",
    "n[:, [0, -1]] = 1\n",
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d70d16d3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 0, 0, 0, 0, 0, 0, 0, 0, 1],\n",
       "       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int16)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方式2\n",
    "n = np.ones((10, 10), dtype=np.int16)\n",
    "n[1:-1, 1:-1] = 0\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cf00b774",
   "metadata": {},
   "source": [
    "6. 创建一个每一行都是从0到4的5*5矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1c6b513a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4],\n",
       "       [0, 1, 2, 3, 4]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#l = np.arange(0,5)\n",
    "l = [0, 1, 2, 3, 4]\n",
    "n = np.array(l * 5).reshape((5, 5))\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee957453",
   "metadata": {},
   "source": [
    "7. 创建一个范围在(0,1)之间的长度为12的等差数列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "bfdd64d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0.        , 0.09090909, 0.18181818, 0.27272727, 0.36363636,\n",
       "       0.45454545, 0.54545455, 0.63636364, 0.72727273, 0.81818182,\n",
       "       0.90909091, 1.        ])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = np.linspace(0, 1, 12)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eccfdb37",
   "metadata": {},
   "source": [
    "8. 创建一个长度为10的随即数组并排列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "1cff7120",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 3, 3, 4, 5, 5, 6])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([6, 5, 5, 4, 3, 3, 3, 2, 1, 0])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n = np.random.randint(0, 10, 10)\n",
    "n.sort()\n",
    "display(n, n[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d365fda",
   "metadata": {},
   "source": [
    "9. 创建一个长度为10的随机数组并将最大值替换为0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e7cdbd30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 9, 6, 9, 8, 4, 8, 0, 4])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "n = np.random.randint(0, 10, size = 10)\n",
    "display(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "88d38861",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([2, 4], dtype=int64)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 找到所有最大值所在的下标\n",
    "max1 = np.max(n)\n",
    "max_indexs = np.argwhere(n==max1).reshape(-1)\n",
    "display(max1, max_indexs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "2652da7b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 1, 0, 6, 0, 8, 4, 8, 0, 4])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n1 = n.copy() # 这不并不是必须，只是为了方便实验\n",
    "n1[max_indexs] = 0\n",
    "n1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
